<template>
  <view class="loginwait">
	  <uni-section title="请选择孩子" type="line" v-show="childrens.length">
			<uni-list>
				<uni-list-item
					v-for="(item,index) in childrens" 
					:title="item.Name" :rightText="item.SchoolName" note="" showArrow clickable  
					@click="chooseChild(item)"
				 />
			</uni-list>
		</uni-section>
		<uni-popup ref="popup3" type="center" before-close="true">
			<view class="popup">
				<view class="popup-close" @click="closepopup()"></view>
				<view class="popup-title" style="font-weight: 600;">{{ popup.title }}</view>
				<view class="popup-text">{{ popup.text }}</view>
				<view class="popup-button" style="background-color: #fff;color:#1492FF;" @click="goHome()">{{ popup.button }}</view>
			</view>
		</uni-popup>
  </view>
</template>

<script>
  import { loginByTicket } from '@/api/login'
  import { getToken, setToken, removeToken,setAccessToken } from '@/utils/auth'
  import { getinfo, setinfo, removeinfo } from '@/utils/auth5'
  import { getChildrens , loginByXZHZCardNo } from '@/api/login'
  export default {
    data() {
      return {
        isApp: this.$store.state.user.isApp,
		ticket: this.$store.state.user.ticket,
		res: null,
		childrens:[],
		access_token:'',
		popup:{
			title: '',
			text: '您选择的学生信息有误，请核查绑定后重新登录。',
			button: '确定',
			url: '',
		},
      }
    },
	beforeCreate(){
		// this.$tab.reLaunch('/pages/index')
	},
    created() {
		let that=this;
		console.log("getinfo()",getinfo())
		// this.isteacher = false
		// if(getinfo().identity == 2){
		// 	this.$nextTick(() => {
		// 		this.isteacher = true
		// 	})
		// }
		let url = window.location.href
		if(url.indexOf('access_token')!=-1){
			// let access_token = url.split("access_token=")[1].split("&")[0]
			let access_token = this.getUrlParam("access_token") || "";
			setAccessToken(access_token);
			this.access_token=access_token;
			// this.$store.state.user.ticket = access_token
			this.$store.state.user.isApp = 1
			// this.$tab.reLaunch('/pages/loginwait')
			let data = {
				"token": access_token
			}
			// console.log("loginByTicket data",data)
			getChildrens(data).then(res => {
				let data = JSON.parse(res.data)
				let loginUser = data.loginUser
				let config = {
				  userId: loginUser.sub, //用户id
				  appId: "1687837298985", //应用id
				};
				console.log("children:", data.children);
				console.log("data:", data);
				console.log("window:", window);
				window.monitorSdk && window.monitorSdk.init(config);
				this.childrens=JSON.parse(data.children);
				// this.childrens=[{
				// 	"Id": 6,
				// 	"ParentAccountId": 36478,
				// 	"Name": "阿一",
				// 	"CardType": 1,
				// 	"CardNumber": "8pb/wELSXqgpCBb67j+6+1cTAMIMZV3NS4nsSeWGrTE=",
				// 	"SchoolId": 0,
				// 	"SchoolName": "啊",
				// 	"AvatorUrl": null
				// }]
				if(this.childrens.length){
					
				}else{
					// uni.showToast({
					//   icon: "none",
					//   title: '您尚未添加孩子',
					// });
					that.$tab.reLaunch('/pages/index')
					// setTimeout(function() {
					// }, 500);
					
				}
				// setToken(res.token)
			 //    this.loginSuccess()
			}).catch(err=>{
				uni.showToast({
				  icon: "none",
				  title: '无效的token!',
				});
			})
		}else{
			uni.showToast({
			  icon: "none",
			  title: '登录信息获取失败!',
			});
		}
      // this.login()
	},
    methods: {
		closepopup() {
		  this.$refs.popup3.close();
		},
		goHome(){
			this.$tab.reLaunch('/pages/index')
		},
		getUrlParam(key) {
		  const url = window.location.search;
		  const reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
		  const result = url.substr(1).match(reg);
		  return result ? decodeURIComponent(result[2]) : "";
		},

		chooseChild(item){
			let params={
				token:this.access_token,
				cardNumber:item.CardNumber
			}
			loginByXZHZCardNo(params).then(res=>{
				setToken(res.token)
				this.loginSuccess()
			}).catch(err=>{
				this.$refs.popup3.open('center');
			})
		},
  //     login(){
		// let data = {
		// 	"isApp": this.isApp,
		// 	"ticket": this.ticket
		// }
		// // console.log("loginByTicket data",data)
  //       loginByTicket(data).then(res => {
		// 	setToken(res.token)
  //           this.loginSuccess()
  //       })
  //     },
	  // 登录成功后，处理函数
	  loginSuccess() {
	    // 设置用户信息
	    this.$store.dispatch('GetInfo').then(res => {
			// let arr = this.$store.state.user.baseUrl.split(':')
			// let burl = arr[0]+':'+arr[1]
			// window.location.href = window.location.origin + window.location.pathname
			this.$tab.reLaunch('/pages/index')
	    })
	  }
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .loginwait{
	  padding:22rpx 10rpx;
  }
  .popup{
  	width: 60vw;
  	height: 40vw;
  	background: #ffffff;
  	position: absolute;
  	margin-left: -30vw;
  	margin-top: -20vw;
  	border-radius: 2.2vw;
  	
  	display: grid;
  	justify-content: center;
  	align-items: center;
  	grid-template-rows: 5vw 5vw 15vw 15vw;
  }
  .popup-close{
  	margin: 2vw 0 0 55vw;
  	width: 3vw;
  	height: 3vw;
  	background: url(../static/index/close.svg);
  	background-size: 100% 100%;
  	cursor: pointer;
  }
  .popup-title{
  	display: grid;
  	justify-content: center;
  	align-items: center;
  	font-weight: 600;
  	font-size: 4vw;
  }
  .popup-text{
  	margin: 0 5vw 0 05vw;
  	width: 50vw;
  	display: grid;
  	justify-content: center;
  	align-items: center;
  	font-size: 3.4vw;
  }
  .popup-button{
  	margin: 0 5vw 0 5vw;
  	width: 50vw;
  	height: 8vw;
  	border-radius: 4vw;
  	display: grid;
  	justify-content: center;
  	align-items: center;
  	color: #ffffff;
  	background: #1492FF;
  	cursor: pointer;
  }
</style>
